package com.mall.b2c.service.impl;

import java.util.Arrays;
import java.util.Date;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.b2c.dao.CommentDao;
import com.mall.b2c.domain.goods.GoodsDomain;
import com.mall.b2c.domain.member.CommentDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.domain.order.OrderItemDomain;
import com.mall.b2c.service.CommentService;
import com.mall.b2c.service.GoodsService;
import com.mall.b2c.service.OrderItemService;
import com.mall.common.exception.NoticeException;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 品牌
 */
@Service("b2cCommentService")
@Transactional
public class CommentServiceImpl extends BaseServiceImpl<CommentDomain, Long> implements CommentService {
	@Resource OrderItemService orderItemService;
	@Resource GoodsService goodsService;
	
	@Resource
	public void setBaseDao(CommentDao commentDao) { 
		super.setBaseDao(commentDao); 
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void comment(Long memberId, Long orderItemId, Integer score, String content, Boolean anonymous, String ip) {
		OrderItemDomain orderItem = orderItemService.find(orderItemId);
		if(orderItem == null) {
			throw new NoticeException("订单项不存在!");
		}
		if(memberId == null) {
			throw new NoticeException("当前用户不存在!");
		}
		MemberDomain member = orderItem.getOrder().getMember();
		if(member == null) {
			throw new NoticeException("订单项未关联用户!");
		}
		if(!member.getId().equals(memberId)) {
			throw new NoticeException("订单项不属于当前用户!");
		}
		CommentDomain comment = new CommentDomain();
		Date now = new Date();
		comment.setCreateDate(now);
		comment.setModifyDate(now);
		comment.setCommentDate(now);
		comment.setUserName(member.getUsername());
		comment.setContent(content);
		comment.setAnonymous(anonymous);
		comment.setScore(score);
		comment.setIp(ip);
		comment.setMember(member);
		GoodsDomain goods = orderItem.getGoods();
		comment.setGoods(orderItem.getGoods());
		orderItem.setComment(comment);
		orderItemService.update(orderItem);
		//修改商品评论数
		String cmtsJson = goods.getCmtsJson();
		JSONObject cmtsJsonObject = JSONObject.fromObject(cmtsJson);// {hignOpinion:0, middleOpinion:0, lowOpinion:0}
		if(score > 3) {//4,5好评
			cmtsJsonObject.put("hignOpinion", cmtsJsonObject.getInt("hignOpinion") + 1);
		} else if(score > 1) {//2,3中评
			cmtsJsonObject.put("middleOpinion", cmtsJsonObject.getInt("middleOpinion") + 1);
		} else {//1差评
			cmtsJsonObject.put("lowOpinion", cmtsJsonObject.getInt("lowOpinion") + 1);
		}
		goods.setId(goods.getId());
		goods.setCmtsJson(cmtsJsonObject.toString());
		goods.setCmts(goods.getCmts() != null ? goods.getCmts() + 1 : 1);
		goodsService.update(goods);
	}

	@Override
	public void remove(Long[] ids, String operator) {
		String hql = "update CommentDomain set isDeleted = :isDeleted , deletePerson = :deletePerson where id in (:ids)";
		getEntityManager().createQuery(hql.toString())
			.setParameter("isDeleted", true)
			.setParameter("deletePerson", operator)
			.setParameter("ids", Arrays.asList(ids))
			.executeUpdate();
	}
}